<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>My JSP 'register.jsp' starting page</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
	$(document).ready(function(){
		var code=document.getElementById("code").value;
		$.ajax({
			url:"http://oauth2-czytest.colourlife.com/oauth/access_token",
			data:{
				application_id:"ICEEANQUAN-E0AE-453B-BEC4-A56980956C6A",
				secret:"6FE3408F971DE183FCD5",
				code:code,
				grant_type:"authorization_code"
			},
			success:function(s1){
				//alert("s1==refresh_token=="+s1.content.refresh_token);
				$.ajax({
					url:"http://oauth2-czytest.colourlife.com/oauth/refresh_token",
					data:{
						application_id:"ICEEANQUAN-E0AE-453B-BEC4-A56980956C6A",
						grant_type:"refresh_token",
						refresh_token:s1.content.refresh_token
					},
					success:function(s2){
						//alert("s2==access_token=="+s2.content.access_token);
						document.getElementById("access_token").value=s2.content.access_token;
						$.ajax({
							url:"https://oauth2-czytest.colourlife.com/oauth/user/info",
							data:{
								access_token:s2.content.access_token
							},
							success:function(s3){
								//alert("===uuid==="+s3.content.community_uuid);
								document.getElementById("community_uuid").value=s3.content.community_uuid;
								$.ajax({
									url:"${basePath}/u/doesitExist",
				  					type:"get",
				  					data:{
				  						userid:s3.content.openid
				  						},
				  					header: {
				        				"content-type": "application/json"
				      				},
				      				success:function(s4){
				      					if(s4==1){
  											//alert("openid已注册，点击前往个人中心");
  											window.location.href="${basePath}/u/home?userid="+s3.content.openid;
  										}else{
  											window.location.href="${basePath}/csh/face.jsp";
  										}
				      				},
				      				error:function(){
				      					//alert("s4 error");
				      				}
								});
							},
							error:function(){
								//alert("s3 error");
							}
						});
					},
					error:function(){
						//alert("s2 error");
					}
				});
			},
			error:function(){
				//alert("s1 error");
			}
		});
	});

</script>

  </head>

  <body>
    This is my JSP page. <br>
    <input id="code" type="hidden" value="<%=request.getParameter("code")%>">
    <input id="access_token" type="hidden" value="token">
    <input id="community_uuid" type="hidden" value="uuid">
  </body>
</html>
